package com.csxy.hql.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csxy.hql.JwtUtils;
import com.csxy.hql.MD5;
import com.csxy.hql.entity.UcenterMember;
import com.csxy.hql.entity.vo.LoginVo;
import com.csxy.hql.entity.vo.RegisterVo;
import com.csxy.hql.exception.MyException;
import com.csxy.hql.mapper.UcenterMapper;
import com.csxy.hql.service.UcenterService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UcenterServiceImpl extends ServiceImpl<UcenterMapper, UcenterMember> implements UcenterService {

    @Autowired
    private UcenterMapper ucenterMapper;




    //登录
    @Override
    public String login(LoginVo loginVo) {
        String mobile = loginVo.getPhone();
        String password = loginVo.getPassword();
        if (StringUtils.isEmpty(mobile) && StringUtils.isEmpty(password)){
            throw new RuntimeException("手机号码和密码不能为空");
        }
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        UcenterMember member = baseMapper.selectOne(queryWrapper);
        if (member == null){
            throw new RuntimeException("手机号码不存在");
        }
        //加密算法验证密码
        if (!MD5.encrypt(password).equals(member.getPassword())){
            throw new RuntimeException("密码错误");
        }

        //判断用户是否禁用
        if (member.getIsDisabled()){
            throw new MyException(20001,"用户被禁用");
        }

        //生成一个额JwtToken
        String token = JwtUtils.getJwtToken(member.getId(), member.getNickname());

        return token;


    }

    @Override
    public void register(RegisterVo registerVo) {
        //获取前端传来的数据
        String nickname = registerVo.getNickname(); //昵称
        String code = registerVo.getCode(); //验证码
        String mobile = registerVo.getMobile(); //手机号
        String password = registerVo.getPassword(); //密码

        //非空判断
        if (StringUtils.isEmpty(nickname)
                || StringUtils.isEmpty(code)
                || StringUtils.isEmpty(mobile)
                || StringUtils.isEmpty(password)){
            throw new MyException(20001,"传来的数据有空值，注册失败");
        }
//        //判断验证码是否正确
//        //获取redis验证码
//        String redisCode = redisTemplate.opsForValue().get(mobile);
//        if (!code.equals(redisCode)){
//            throw new MyException(20001,"验证码错误，注册失败");
//        }
        if (!code.equals("7777")){
            throw new MyException(20001,"验证码错误，注册失败");
        }
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        Long count = baseMapper.selectCount(queryWrapper);
        if (count >= 1){
            throw new MyException(20001,"手机号重复，注册失败");
        }
        //数据添加到数据库中
        UcenterMember member = new UcenterMember();
        member.setPassword(MD5.encrypt(password));  //密码进行md5加密
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setIsDisabled(false); //用户不禁用
        member.setAvatar("https://ts1.tc.mm.bing.net/th/id/OIP-C.aCW_Ax9FtUGhnDBetciYoQAAAA?rs=1&pid=ImgDetMain&o=7&rm=3");
        baseMapper.insert(member);
    }

    @Override
    public Integer getCountRegister(String day) {
        return ucenterMapper.getCountRegister(day);
    }
}
